package com.sjf.reggie.controller;

import com.sjf.common.Result;
import com.sjf.reggie.entity.ShoppingCart;
import com.sjf.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author <huangtang>
 * @since 2022/4/27 13:21
 **/
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {


    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCart> add( @RequestBody ShoppingCart shoppingCart){
        log.info ("添加购物车:{}",shoppingCart);
        return Result.success (shoppingCartService.add(shoppingCart));
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result<ShoppingCart> sub( @RequestBody ShoppingCart shoppingCart){
        log.info ("从购物车中删除商品:{}",shoppingCart);
        return Result.success (shoppingCartService.sub(shoppingCart));
    }

    @DeleteMapping("/clean")
    public Result<String> clean(){
        log.info ("清空购物车");
        shoppingCartService.clean();
        return Result.success ();
    }

    /**
     * 获取当前用户购物车列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        List<ShoppingCart> list =  shoppingCartService.getList();
        return Result.success (list);
    }
}
